package com.oa.co.controller;

import com.github.pagehelper.PageInfo;
import com.oa.co.common.PageVo;
import com.oa.co.common.R;
import com.oa.co.pojo.Room;
import com.oa.co.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author Lone Ranger
 */
@RestController
@CrossOrigin("*")
@RequestMapping("/api/room")
public class RoomController {

    @Autowired
    private RoomService roomService;

    // 查询所有的会议室  （用于联动信息）
    @RequestMapping("/getrooms")
    public R getRooms() {
        List<Room> rooms = roomService.getRooms();
        return R.ok().data("rooms", rooms);
    }

    // 查询所有的会议室
    @RequestMapping("/allroom")
    public PageVo<Room> roomList(Integer page, Integer limit, Integer floor, Integer room_status) {

        PageVo<Room> pageVo = new PageVo<>();
// mybatis的分页插件
        PageInfo<Room> pageData = roomService.getAllRoom(page, limit, floor, room_status);
        pageVo.setData(pageData.getList());
        pageVo.setCode(0);
        pageVo.setCount(pageData.getTotal());
        pageVo.setMsg("会议室信息");
        return pageVo;
    }

    // 添加会议室
    @RequestMapping("/addRoom")
//    public R addRoom(String room_name,Integer floor,Integer max_num) {
    public R addRoom(@RequestBody Room room) {

//        System.out.println("添加的  "+room);
        // 0表示会议室空闲
        room.setRoom_status(0);
//        Integer num = roomService.addRoom(room_name, floor, max_num);
        Integer num = roomService.addRoom(room);

        if (num == 1) {
            return R.ok().message("添加成功");
        }
        return R.error().message("添加失败");
    }

    // 删除会议室
    @RequestMapping("/deleteRoom")
    public R deleteRoom(Integer room_id) {
        Integer num = roomService.deleteRoom(room_id);
        if (num == 1) {
            return R.ok().message("删除成功");
        }
        return R.error().message("删除失败");
    }

    // 批量删除会议室
    @RequestMapping("/deleteRooms")
    public R deleteRooms(@RequestBody List<Room> list) {

        Integer num = roomService.deleteRoomMulti(list);
        if (num == list.size()) {
            return R.ok().message("删除成功");
        }
        return R.error().message("删除失败");
    }

    @RequestMapping("/editRoom")
    public R editRoom(@RequestBody  Room room) {
//        System.out.println("更新数据" + room);
        Integer num = roomService.updateRoom(room);
        if (num == 1) {
            return R.ok().message("更新成功");
        }
        return R.error().message("更新失败");
    }

}
